package com.estate.owner.controller;

import com.estate.bean.Car;
import com.estate.owner.service.CarService;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/car")
public class CarController {

    @Resource
    private CarService carService;

    @GetMapping("/list/{carId}")
    public JsonResult findById(@PathVariable("carId") String carId) {
        return carService.findById(carId);
    }

    @GetMapping("/listByPage/{page}/{size}")
    public JsonResult listByPage(@PathVariable("page") int page, @PathVariable("size") int size) {
        log.info("page:{},size:{}", page, size);
        JsonResult result = carService.listByPage(page, size);
        log.info("result:{}", result);
        return ResultTool.success(result);
    }

    @PostMapping("/save")
    public JsonResult save(Car car) {
        log.info("car:{}", car);
        JsonResult result = carService.addCar(car);
        log.info("result:{}", result);
        return ResultTool.success(result);
    }

    @PutMapping("/update")
    public JsonResult update(Car car) {
        log.info("car:{}", car);
        JsonResult result = carService.updateCar(car);
        log.info("result:{}", result);
        return ResultTool.success(result);
    }

    @DeleteMapping("/delete")
    public JsonResult delete(String id) {
        log.info("id:{}", id);
        return carService.deleteCar(id);
    }
}
